package com.madding.shared.net.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Server {

    ServerSocket       providerSocket;
    Socket             connection = null;

    ObjectOutputStream out;
    ObjectInputStream  in;

    String             control;
    String             inmessage;
    String             outmessage;

    SimpleDateFormat         sdf;

    Server(){
        try {
            // 1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);
        } catch (IOException e) {
            e.printStackTrace();
        }

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    void destory() {
        try {
            // 4: Closing connection
            if (providerSocket != null) {
                providerSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void run() {
        if (providerSocket == null) {
            System.exit(1);
        }

        try {
            // 2. Wait for connection
            System.out.println("Waiting for connection");
            connection = providerSocket.accept();
            System.out.println("Connection received from " + connection.getInetAddress().getHostName());
            // 3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());

            control = "start";
            sendMessage();
            // 4. The two parts communicate via the input and output streams
            do {
                try {
                    displayMessage();

                    setControl();

                    sendMessage();
                } catch (ClassNotFoundException classnot) {
                    System.err.println("Data received in unknown format");
                }
            } while (!control.equals("bye") && !connection.isClosed());
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
                connection.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    void setControl() {
        control = "";
        if (inmessage.equals("bye")) {
            control = "bye";
        } else if(inmessage.equals("date")) {
            control = "date";
        } else if(inmessage.equals("help")) {
            control = "help";
        }
    }

    void displayMessage() throws IOException, ClassNotFoundException {
        inmessage = (String) in.readObject();
        System.out.println("client>" + inmessage);
    }

    void sendMessage() throws IOException {
        if (control.equals("start")) {
            outmessage = "Connection successful";
        } else if (control.equals("date")) {
            outmessage = sdf.format(new Date());
        } else if (control.equals("help")) {
            outmessage = "help will support letter.";
        }

        try {
            if (outmessage == null || "".equals(outmessage)) {
                outmessage = "error input.";
            }
            out.writeObject(outmessage);
            out.flush();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        } finally {
            outmessage = "";
        }
    }

    public static void main(String args[]) {
        Server server = new Server();
        while (true) {
            server.run();
        }
    }
}
